﻿using System.Linq;
using Roslyn.Compilers.CSharp;

namespace YieldReturnYieldReturnManyReturn
{
    internal class YieldReturnAndreturnMethodVisitor : SyntaxVisitor<bool?>
    {
        private bool _hasReturn;
        private bool _hasYieldReturn;

        public override bool? Visit(SyntaxNode node)
        {
            return base.Visit(node) ?? node.ChildNodes().Any(cn => Visit(cn) == true);
        }

        protected override bool? VisitMethodDeclaration(MethodDeclarationSyntax node)
        {
            _hasReturn = false;
            _hasYieldReturn = false;
            base.VisitMethodDeclaration(node);
            foreach (var cn in node.ChildNodes())
                Visit(cn);
            if (_hasReturn && _hasYieldReturn)
                return true;
            return false;
        }

        protected override bool? VisitReturnStatement(ReturnStatementSyntax node)
        {
            _hasReturn = true;
            return false;
        }

        protected override bool? VisitYieldStatement(YieldStatementSyntax node)
        {
            _hasYieldReturn = true;
            return false;
        }

        protected override bool? VisitParenthesizedLambdaExpression(ParenthesizedLambdaExpressionSyntax node)
        {
            return false;
        }

        protected override bool? VisitAnonymousMethodExpression(AnonymousMethodExpressionSyntax node)
        {
            return false;
        }
    }
}
